#!/bin/sh
### BEGIN INIT INFO
# Provides:          pdns-recursor
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: PowerDNS Recursor - Recursive DNS Server
# Description:       PowerDNS Recursor - Recursive DNS Server
### END INIT INFO

#
# Authors:	Matthijs Möhlmann <matthijs@cacholong.nl>
#           Christoph Haas <haas@debian.org>
# 
# Thanks to:
# Thomas Hood <jdthood@aglu.demon.nl>
#
# initscript for PowerDNS recursor

. /lib/lsb/init-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC="PowerDNS recursor"
NAME=pdns_recursor
DAEMON=/usr/sbin/$NAME
# Derive the socket-dir setting from /etc/powerdns/recursor.conf
# or fall back to the default /var/run if not specified there.
PIDDIR=$(awk -F= '/^socket-dir=/ {print $2}' /etc/powerdns/recursor.conf)
if [ -z "$PIDDIR" ]; then PIDDIR=/var/run; fi
PIDFILE=$PIDDIR/$NAME.pid

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

# Read config file if it is present.
if [ -r /etc/default/pdns-recursor ]; then
  . /etc/default/pdns-recursor
fi

start() {
# Return
#  0 if daemon has been started / was already running
#  >0 if daemon could not be started
  start-stop-daemon --start --oknodo --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null || return 0
  start-stop-daemon --start --oknodo --quiet --pidfile $PIDFILE --exec $DAEMON || return 2
}

start_resolvconf() {
  if [ "X$RESOLVCONF" = "Xyes" ] && [ -x /sbin/resolvconf ]; then
    echo "nameserver 127.0.0.1" | /sbin/resolvconf -a lo.pdns-recursor
  fi
  return 0
}

stop() {
# Return
#  0 if daemon has been stopped
#  1 if daemon was already stopped
#  2 if daemon could not be stopped
#  other if a failure occured
  start-stop-daemon --stop --quiet --retry=HUP/30/TERM/5/KILL/5 --pidfile $PIDFILE --name $NAME
  RETVAL="$?"
  [ "$RETVAL" = 2 ] && return 2
  rm -f $PIDFILE
  return "$RETVAL"
}

stop_resolvconf() {
  if [ "X$RESOLVCONF" = "Xyes" ] && [ -x /sbin/resolvconf ]; then
    /sbin/resolvconf -d lo.pdns-recursor
  fi
  return 0
}

case "$1" in
  start)
    if [ "$START" != "yes" ]; then
      log_begin_msg "Not starting $DESC -- disabled."
      log_end_msg 0
      exit 0
    fi
    log_daemon_msg "Starting $DESC" "pdns-recursor"
    start
    case "$?" in
      0)
        start_resolvconf
        break
        ;;
      1)
        log_progress_msg "(already running)"
        break
        ;;
      *)
        log_progress_msg " (failed)."
        log_end_msg 1
        exit 1
        ;;
    esac
    log_end_msg 0
  ;;
  stop)
    stop_resolvconf
    log_daemon_msg "Stopping $DESC" "pdns-recursor"
    stop
    case "$?" in
      0)
        break
        ;;
      1)
        log_progress_msg "(not running)"
        break
        ;;
      *)
        log_progress_msg "(failed)"
        log_end_msg 1
        exit 1
        ;;
    esac
    log_end_msg 0
  ;;
  restart|force-reload)
    if [ "$START" != "yes" ]; then
      $0 stop
      exit 0
    fi
    log_daemon_msg "Restarting $DESC" "pdns-recursor"
    stop
    case "$?" in
      0|1)
        start
        case "$?" in
          0)
            log_end_msg 0
            exit 0
            ;;
          1)
            log_progress_msg "(failed -- old process still running)"
            log_end_msg 1
            exit 1
            ;;
          *)
            log_progress_msg "(failed to start)"
            log_end_msg 1
            exit 1
            ;;
        esac
      ;;
      *)
        log_progress_msg "(failed to stop)"
        log_end_msg 1
        exit 1
      ;;
    esac
  ;;
  force-stop)
    killall -v -9 pdns_recursor
    echo "killed"
  ;;
  status)
    status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
  ;;
  *)
    echo "Usage: $0 {start|stop|restart|force-reload|force-stop|status}" >&2
    exit 3
  ;;
esac

exit 0

